//
//  DDDownloadFile.m
//  MyClouds
//
//  Created by sarah doo on 13-8-11.
//  Copyright (c) 2013年 sarah doo. All rights reserved.
//

#import "DDDownloadFile.h"

@implementation DDDownloadFile



- (id)initWithData:(NSData *)data {
    self = [super initWithData:data];
    return self;
}

- (id)initWithUid:(int)uid andOffset:(long)off andLength:(long)len{
    self = [super init];
    NSData* uIdData = [NSData dataWithBytes:&uid length:sizeof(uid)];
    NSRange range;
    range.location = HEADER_LENGTH;
    range.length = INT_SIZE;
    [[self entity]replaceBytesInRange:range withBytes:[uIdData bytes]];

    NSData* offData = [NSData dataWithBytes:&off length:sizeof(off)];
    range.location = range.location + range.length;
    range.length = LONG_SIZE;
    [[self entity]replaceBytesInRange:range withBytes:[offData bytes]];
    
    NSData* lenData = [NSData dataWithBytes:&len length:sizeof(len)];
    range.location = range.location + range.length;
    range.length = LONG_SIZE;
    [[self entity]replaceBytesInRange:range withBytes:[lenData bytes]];
    [self setCmd:CloudCmd_DownloadFile];
    [self setLength:[entity length]];
    int crc = [self crcs:entity withLength:[entity length]];
    [self setCrc:crc];
    return self;
}

- (int)getUid{
    int uid = -1;
    NSRange range;
    range.location = HEADER_LENGTH;
    range.length = INT_SIZE;
    NSData* uidData = [[self entity]subdataWithRange:range];
    [uidData getBytes:&uid length:sizeof(uid)];
    return uid;
}

- (long)getOffset{
    long offset = -1;
    NSRange range;
    range.location = HEADER_LENGTH + INT_SIZE;
    range.length = LONG_SIZE;
    NSData* offsetData = [[self entity]subdataWithRange:range];
    [offsetData getBytes:&offset length:LONG_SIZE];
    return offset;
}

- (long)getLength{
    long length = -1;
    NSRange range;
    range.location = HEADER_LENGTH + INT_SIZE + LONG_SIZE;
    range.length = LONG_SIZE;
    NSData* lengthData = [[self entity]subdataWithRange:range];
    [lengthData getBytes:&length length:LONG_SIZE];
    return length;
}

@end
